// pages/nurse/mypatient/mypatient.js
var api = require('../../../config/api.js');
const util = require('../../../utils/util.js');

//获取应用实例
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    patient_names:[
    ],
    patient_item:[
      {
        patientId: 0,
        name: "王小明",
        age: " 28岁",
        sex: "男",
        status: " 已出院",
        userId: 0
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getPatientNames()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getPatientNames: function() {
    let that = this;
    let patient_names = that.data.patient_names;
    console.log("getPatientNames app.globalData.metaId" + app.globalData.metaId);
     util.request(api.MyPatientInfoList,{
      nurseId: app.globalData.metaId
      }).then(function (res) {
          console.log("getPatientNames" + JSON.stringify(res));
          var patient_names_obj = res['data'];

          var temp_patient_names = [];
          if (res.errno === 0) {
            console.log("getPatientNames res data" + JSON.stringify(res['data']));
            for (var i = 0; i < patient_names_obj.length; i++) {
              console.log("getPatientNames patient_names_obj[i]['id'] " + patient_names_obj[i]['id']);

              var patient_name_todo='patient_names['+i+'].name';
              that.setData({
                [patient_name_todo]:patient_names_obj[i]['name']
              })
              var patient_id_todo='patient_names['+i+'].patientId';
              that.setData({
                [patient_id_todo]:patient_names_obj[i]['id']
              })
              var patient_sex_todo='patient_names['+i+'].sex';
              that.setData({
                [patient_sex_todo]:patient_names_obj[i]['sex'] == 0 ? '男':'女'
              })
              var patient_sex_todo='patient_names['+i+'].userId';
              that.setData({
                [patient_sex_todo]:patient_names_obj[i]['userId'] = patient_names_obj[i]['userId']
              })
              var patient_status_todo='patient_names['+i+'].status';
              that.setData({
                [patient_status_todo]:patient_names_obj[i]['status']
              })
              var patient_age_todo='patient_names['+i+'].age';
              that.setData({
                [patient_age_todo]:util.getAge(patient_names_obj[i]['birthday'])
              })
            }
              console.log("getPatientNames patient_names final " + that.data.patient_names);
          }
    });

    /*this.setData({
      patient_names:[
        {
          id: 0,
          name: "王小明",
          age: " 28岁",
          sex: "男",
          status: " 已出院"
        },
        {
          id: 0,
          name: "王小明",
        },
        {
          id: 0,
          name: "王小明",
        },
        {
          id: 0,
          name: "王小明",
        },
        {
          id: 0,
          name: "王小明",
        },
        {
          id: 0,
          name: "王小明",
        },
        {
          id: 0,
          name: "王小明",
        },
        {
          id: 0,
          name: "王小明",
        },
        {
          id: 0,
          name: "王小明",
        },
        {
          id: 0,
          name: "王小明",
        },
        {
          id: 0,
          name: "王小明",
        }

      ]
    });*/
  }
})